package com.example.mongodb.mongodb.controller.rabbitmq.toupic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeoutException;

public class EmitLogTopic1 {

    private static final String EXCHANGE_NAME ="topic_logs";

    private static final String[] SPEED={"lazy","quick","normal"};
    private static final String[] COLOR={"black","orange","red","yellow","blue","white","pink"};
    private static final String[] SPECIES={"dog","rabbit","chicken","horse","bear","cat"};

    private static final Random RANDOM = new Random();


    public static void main(String[] args) {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("123.207.179.78");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");

        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();

            //申明一个主题模式的交换机
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
            String message = null;
            String routingKey = null;
            String speed = null;
            String color = null;
            String species = null;

            for (int i = 0; i < 10; i++) {
                speed = "lazy";
                color = COLOR[RANDOM.nextInt(COLOR.length)];
                species = SPECIES[RANDOM.nextInt(SPECIES.length)];
                message = speed+ "-"+species;
                routingKey = speed+ "."+species;

                channel.basicPublish(EXCHANGE_NAME,routingKey,null,message.getBytes());

                System.out.println("[]Sent "+routingKey + ": "  +message);

            }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
